<template>
  <div class="header">
    <div class="header_left">
      <h2>叩丁狼电商运营平台</h2>
      <el-icon size="20" @click="store.changeCollapse(!store.collapse)"
        ><Menu
      /></el-icon>
    </div>
    <div class="header_right">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{ useStore.username }}<el-icon color=""><ArrowDown /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="index">首页</el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { Menu, ArrowDown } from "@element-plus/icons-vue";
import { useHome } from "@/store/home";
import { useUserKey } from "@/store/user";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { relogin } from "@/common";
import { logoutAPI } from "@/http/api/index";

const useStore = useUserKey();
const store = useHome();
const router = useRouter();
const handleCommand = (command: string) => {
  if (command === "index") {
    // 跳转首页
    router.push("/index");
  } else if (command === "logout") {
    relogin(
      () => {
        logoutAPI().then((res) => {
          console.log(res);
        });
      },
      () => {
        ElMessage.success("退出成功");
      }
    );
  }
};
</script>

<style lang="less" scoped>
.header {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .header_left {
    display: flex;
    align-items: center;
    color: #fff;
    h2 {
      padding-right: 20px;
      font-weight: normal;
    }
  }
  .header_right {
    :deep(.el-dropdown-link) {
      color: #fff;
    }
  }
}
</style>
